<template>
	<!-- 设置 -->
	<view>
		<!-- #ifdef APP-PLUS -->
		<view class="status_bar"></view>
		<!-- #endif -->
		<navbar title="设置" />
		<view class="main">
			<navigator class="item" url="./setup_userinfo">
				<view class="title">基本信息</view>
				<image src="/static/images/more.png"></image>
			</navigator>
			<view class="line"></view>
			<navigator class="item" url="./aboutUs">
				<view class="title">关于我们</view>
				<image src="/static/images/more.png"></image>
			</navigator>
			<view class="line"></view>
			<navigator class="item" url="./userAgreement">
				<view class="title">用户服务协议</view>
				<image src="/static/images/more.png"></image>
			</navigator>
			<view class="line"></view>
			<navigator class="item" url="./privacyPolicy">
				<view class="title">隐私保护政策</view>
				<image src="/static/images/more.png"></image>
			</navigator>
			<view class="line"></view>
			<navigator class="item" url="/pages/mine/feedback">
				<view class="title">我要反馈</view>
				<image src="/static/images/more.png"></image>
			</navigator>
			<view class="line"></view>
			<navigator class="item" url="/pages/login/register/userRegistrationAgreement" @click="clearStorage">
				<view class="title">缓存清理</view>
				<text>清理缓存</text>
			</navigator>
			<view class="line"></view>
			<view class="item">
				<view class="title">当前版本</view>
				<text>{{version}}</text>
			</view>
			<!-- <view class="item" @click="checkUpdate()">
				<view class="title">检查新版本</view>
				<image src="/static/image/home/more.png" mode=""></image>
			</view> -->
		</view>
		<view class="cancellationbtn" @click="cancellation">注销账户</view>
		<view class="logoutbtn" @click="logout">退出登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				version: '1.0.0' //版本号
			}
		},
		onLoad() {
			console.log(uni.getSystemInfoSync());
			this.version = uni.getSystemInfoSync().appVersion;
		},
		onShow() {
		   if (!uni.getStorageSync('token')) {
		    uni.showModal({
		     title: '温馨提示',
		     content: '请登录以继续查看',
		     showCancel: false,
		     success: (res) => {
		      if (res.confirm) {
		       uni.navigateTo({
		        url: '/pages/login/login'
		       })
		      }
		     }
		    })
		   }
		  },
		methods: {
			//清理缓存
			clearStorage() {
				uni.showLoading({
					title: '清理中...'
				})
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径 
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录 
									uni.hideLoading();
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							uni.hideLoading();
							uni.showToast({
								title: '清理失败，稍后再试',
								duration: 2000
							});
							console.log('文件路径读取失败')
						});
					}
				} else { // ios 
					plus.cache.clear(function() {
						uni.hideLoading();
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
					});
				}
			},
			//注销账户（伪注销）
			cancellation() {
				uni.showModal({
					title: '温馨提示',
					content: '您正在进行账户注销操作，确定注销后您的账户数据及收益将被清零，是否确定注销？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title:'注销中...'
							})
							setTimeout(()=>{
								uni.showToast({
									title:'您的账户已注销！',
									icon:'none',
									duration:2000
								})
								setTimeout(()=>{
									uni.removeStorageSync('token');
									uni.reLaunch({
										url:'/pages/login/login'
									})
								},2000)
							},2000)
						}
					}
				})
			},
			//退出登录
			logout() {
				uni.showModal({
					title: '温馨提示',
					content: '确认退出登录？',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('token');
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding: calc(var(--status-bar-height) + 80rpx) 0 30rpx;
	}

	.main {
		width: 690rpx;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		margin: 50rpx 30rpx;
		padding: 0 30rpx;
		box-sizing: border-box;

		.item {
			@include between;
			height: 100rpx;

			.title {
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: normal;
				color: #000000;
			}

			image {
				width: 16rpx;
				height: 30rpx;
			}

			text {
				font-size: 24rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #666666;
			}
		}

		.line {
			width: 630rpx;
			height: 1rpx;
			background-color: #E5E5E5;
		}
	}
	.cancellationbtn{
		width: 560rpx;
		height: 70rpx;
		background: #CCCCCC;
		border-radius: 35rpx;
		@include center;
		position: fixed;
		bottom: 430rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 999;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
	}
	.logoutbtn {
		width: 560rpx;
		height: 70rpx;
		background: linear-gradient(180deg, #FF8583 0%, #FF660A 100%);
		border-radius: 35rpx;
		@include center;
		position: fixed;
		bottom: 280rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 999;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
	}
</style>
